/*
 Copyright (c) 2013, Ike Yousuf <admin@kudodev.com>
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer in the
 documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 names of its contributors may be used to endorse or promote products
 derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.kudo.gdx.ui.components;

import com.kudo.gdx.Graphics;
import com.kudo.gdx.ui.Container;
import com.kudo.gdx.ui.Skin;
import com.kudo.gdx.ui.event.MouseListener;

/**
 *
 * @author Ike
 */
public class VerticalScrollBar extends Container {

    private int value;
    private Button thumb;
    private boolean clicked = false;
    private String activeTrack = "activeTrackBlue", inactiveTrack = "inactiveTrackBlue", disabledTrack = "disabledTrackBlue";
    private boolean active = false;

    public VerticalScrollBar(int trackHeight, int thumbHeight, int thickness, float x, float y) {
        super(x, y, thickness, trackHeight);
        this.thumb = new Button(0, ((trackHeight / 2) - (thumbHeight / 2)), thickness, thumbHeight);
        createThumb();
        getInput().addProcessor(new ScrollBarListener());
        add(this.thumb);
    }

    @Override
    public void render(Graphics g) {
        if (active && getEnabled()) {
            g.draw(Skin.instance().getScrollBar(activeTrack), getBounds().x, getBounds().y, getBounds().width, getBounds().height);
        } else if (!getEnabled()) {
            g.draw(Skin.instance().getScrollBar(disabledTrack), getBounds().x, getBounds().y, getBounds().width, getBounds().height);
        } else {
            g.draw(Skin.instance().getScrollBar(inactiveTrack), getBounds().x, getBounds().y, getBounds().width, getBounds().height);
        }
        renderChildren(g);
    }

    private class ScrollBarListener extends MouseListener {

        @Override
        public boolean touchUp(int screenX, int screenY, int pointer, int button) {
            if (clicked) {
                clicked = false;
                active = false;
                return true;
            }
            return false;
        }
    }

    private void createThumb() {
        this.thumb.getInput().addProcessor(0, new MouseListener() {
            @Override
            public boolean touchDown(int screenX, int screenY, int pointer, int button) {
                if (thumb.contains(screenX, screenY)) {
                    clicked = true;
                    active = true;
                    return true;
                } else {
                    active = false;
                    clicked = false;
                    return false;
                }
            }

            @Override
            public boolean touchDragged(int screenX, int screenY, int pointer) {
                if (clicked) {
                    thumb.setY(screenY - (thumb.getHeight() / 2));
                    value = (int) (((thumb.getY() - getY()) / (getBounds().getHeight() - thumb.getHeight())) * 100);

                    if (getBounds().getHeight() == thumb.getHeight()) {
                        thumb.setY(getY());
                        value = 1;
                    } else if (screenY <= getY() + (thumb.getHeight() / 2)) {
                        thumb.setY(getY());
                        value = 1;
                    } else if (screenY >= getY() + getBounds().getHeight() - (thumb.getHeight() / 2)) {
                        thumb.setY(getY() + getBounds().getHeight() - thumb.getHeight());
                        value = 100;
                    }
                    return true;
                } else {
                    return false;
                }
            }
        });
    }
}
